package yuekao2.util;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import yuekao2.entity.Tm1_3_6;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class ClickHouseUtil  extends RichSinkFunction<Tm1_3_6> {
    Connection conn = null;
    PreparedStatement ps = null;
    @Override
    public void open(Configuration parameters) throws Exception {
        //1.加载驱动类
        Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
        //2.得到连接对象
        conn = DriverManager.getConnection("jdbc:clickhouse://hadoop-single:8123", "default", "");
    }

    @Override
    public void close() throws Exception {
        ps.close();
        conn.close();
    }

    @Override
    public void invoke(Tm1_3_6 value, Context context) throws Exception {
        PreparedStatement ps = conn.prepareStatement("insert into yk2_1_3_6 values (?,?,?,?,?,?)");
        ps.setObject(1,value.getWstart());
        ps.setObject(2,value.getWend());
        ps.setObject(3,value.getSource());
        ps.setObject(4,value.getWord());
        ps.setObject(5,value.getCnt());
        ps.setObject(6,value.getTs());
        ps.execute();
    }
}
